import { Component, OnInit, ViewChild, Output, EventEmitter, ViewChildren, Input } from '@angular/core';
import { appSynonymUrl } from '../../synonym.url';
import { CudTableService } from 'app/public/serve/cud-table/cud-table.service';
import { Service } from '../../../server/server';
import { synonym } from '../../synonym.data';
import { HintsService } from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from '../../../public/serve/remove-table/remove-table.service';
import { InitService } from '../../../public/serve/init/init.service';
import { AngularWaitBarrier } from 'blocking-proxy/built/lib/angular_wait_barrier';
import { SynonymManagementTableComponent } from '../synonym-management-table/synonym-management-table.component';
import { viewClassName } from '@angular/compiler';
import * as pinyintool from 'pinyin-tool';
//import 'pinyin4js';
//require("pinyin4js");

@Component({
  selector: 'app-synonym-management-left',
  templateUrl: './synonym-management-left.component.html',
  styleUrls: ['./synonym-management-left.component.css']
})
export class SynonymManagementLeftComponent implements OnInit {

  tabVal: any;
  @Output() onSubmit = new EventEmitter<void>();

  @Input()
  tableDataLeft: EventEmitter<any> = new EventEmitter();
  @Input() searchData: any;
  @Input()
  public leftTableData : any;

  constructor(
    private server: Service,
    public request: CudTableService,
    private tableListService: CudTableService,
    private hintsService: HintsService,
    private removeTableService: RemoveTableService,
    public initService: InitService
  ) {

  }

  ngOnInit() {
      let result = this.leftTableData;
      this.tabVal = result;

    // this.getLeftTabList();
      // console.log(this.leftTableData);

  }

  updateData(){
      let result = this._pinYinTitle(this.leftTableData);
      this.tabVal = result;
  }

  //提取分组拼音
  _pinYinTitle(val:any){

    // require("pinyin4js");
    let tmp = {
      'A':[],
      'B':[],
      'C':[],
      'D':[],
      'E':[],
      'F':[],
      'G':[],
      'H':[],
      'I':[],
      'J':[],
      'K':[],
      'L':[],
      'M':[],
      'O':[],
      'P':[],
      'Q':[],
      'R':[],
      'S':[],
      'T':[],
      'U':[],
      'V':[],
      'W':[],
      'X':[],
      'Y':[],
      'Z':[],
      '#':[]
    };
    val.forEach(function (p, v) {
        let keywords = p['keywords'];
        if(keywords !== undefined){
            let py = pinyintool.chineseToPinyin(keywords);
            let pyTitle = py[0].toUpperCase();
            if(tmp.hasOwnProperty(pyTitle)){
                tmp[pyTitle].push(keywords);
            }else{
                tmp['#'].push(keywords);
            }
        }
    });

    return tmp;
  }

 
  //搜索
  search() {
    synonym.retrieval.queryCriteria = this.searchData;
    this.onSubmit.emit();
  }
  //响应搜索
  changesearch() {
      this.search();
  }

  _leftRetrieval(v){
    this.search();
  }

   /**
    * 左侧同义词列表
    */
  getLeftTabList(){
    let data = this._tableData();
    this.request.postRequest( this.server.baseUrl + 'Service/ThemeWords/getThemeWordsList',data).then((res:any) => {
      if (res.result_code === '0000') {
        let pyResult = this._pinYinTitle(res.result);
        this.tabVal = pyResult;
      }else{
        this.hintsService.messages(res.message,res.result_code);
      }

      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }

  goTo(location: string): void {
      let obj = document.getElementById(location);
      if(obj !== undefined){
          document.getElementById(location).scrollIntoView(true);
      }
  }

  //查询参数
  private _tableData(){
    return {
      token         : synonym.retrieval.token,
      pageNo        : synonym.pagination.pageNumber,
      pageSize      : 200,
      startTime     : synonym.retrieval.startTime,
      endTime       : synonym.retrieval.endTime,
      queryCriteria : synonym.retrieval.queryCriteria
    }
  }

}
